package com.qf.smsplatform.controller;

import com.qf.smsplatform.service.CacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@RestController
@Api(tags = "缓存模块核心Controller")
@Slf4j
public class CacheController {

    @Autowired
    private CacheService cacheService;

    @RequestMapping(value = "/cache/hmset/{key}", method = RequestMethod.POST)
    public boolean hMSet(@PathVariable("key") String key, @RequestBody Map map){
        log.info("【缓存模块-hmset】 存储数据到hash结构  key = {}，map = {}",key,map);
        cacheService.hMSet(key,map);
        log.info("【缓存模块-hmset】 存储数据到hash结构  操作成功！！！");
        return true;
    }


    @GetMapping(value = "/cache/hgetall/{key}")
    public Map hGetAll(@PathVariable String key){
        log.info("【缓存模块-hGetAll】 查询hash结构全部数据  key = {}",key);
        Map result = cacheService.hGetAll(key);
        log.info("【缓存模块-hGetAll】 成功查询到hash结构全部数据  key = {}，map = {}",key,result);
        return result;
    }

    @GetMapping("/cache/hget/{key}/{field}")
    public Object hGet(@PathVariable  String key, @PathVariable String field){
        log.info("【缓存模块-hGet】 查询hash结构中的value  key = {},field = {}",key,field);
        Object value = cacheService.hGet(key,field);
        log.info("【缓存模块-hGet】 查询hash结构中的value  key = {},field = {},value = {}",key,field,value);
        return value;
    }

    @RequestMapping(value = "/cache/save2cache-persist", method = RequestMethod.POST)
    public boolean saveCache(@RequestParam("key") String key, @RequestParam("value") String value){
        log.info("【缓存模块-saveCache】 向String结构存储Key-Value  key = {},value = {}",key,value);
        cacheService.saveCache(key,value);
        log.info("【缓存模块-saveCache】 存储数据到String结构成功!  key = {},value = {}",key,value);
        return true;
    }

    @RequestMapping(value = "/cache/get/{key}", method = RequestMethod.GET)
    public Object get(@PathVariable("key") String key){
        log.info("【缓存模块-get】 从String结构获取Value  key = {}",key);
        Object value = cacheService.get(key);
        log.info("【缓存模块-get】 从String结构获取Value  key = {},value = {}",key,value);
        return value;
    }

    @RequestMapping(value = "/cache/sadd/{key}", method = RequestMethod.POST)
    public boolean sadd(@PathVariable String key, @RequestBody Map map){
        log.info("【缓存模块-sadd】 向set结构存储数据  key = {},map = {}",key,map);
        cacheService.sadd(key,map);
        log.info("【缓存模块-sadd】 向set结构存储数据成功  key = {},map = {}",key,map);
        return true;
    }

    @RequestMapping(value = "/cache/smembers/{key}", method = RequestMethod.GET)
    public Set<Object> smembers(@PathVariable String key){
        log.info("【缓存模块-smembers】 获取Set结构中的全部数据  key = {}",key);
        Set<Object> value = cacheService.smembers(key);
        log.info("【缓存模块-smembers】 获取Set结构中的全部数据success  key = {}, value = {}",key,value);
        return value;
    }


    @RequestMapping(value = "/cache/sadd-string/{key}", method = RequestMethod.POST)
    public boolean saddString(@PathVariable String key, @RequestBody String... values){
        log.info("【缓存模块-sadd-string】 向set结构存储数据  key = {},values = {}",key,values);
        cacheService.saddString(key,values);
        log.info("【缓存模块-sadd-string】 向set结构存储数据成功  key = {},values = {}",key,values);
        return true;
    }

    @PostMapping("/cache/hincr/{key}/{field}/{value}")
    public Long hincr(@PathVariable String key,@PathVariable String field,@PathVariable long value){
        log.info("【缓存模块-hincr】 增加hash结构中指定filed对应的value  key = {},field = {},value = {}",key,field,value);
        Long result = cacheService.hincr(key,field,value);
        log.info("【缓存模块-hincr】 增加hash结构中指定filed对应的value  key = {},field = {},value = {}",key,field,value);
        return result;
    }


}
